﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace PipePrinter
{
    public partial class frmMain : Form
    {
        private bool inProcess = false;
        private bool sysConfiged = false;

        private PrinterProtocol protocol;

#if DEBUG
        private TraceForm traceform;
#endif

        public frmMain()
        {
            InitializeComponent();

            this.Icon = Properties.Resources.Pipe;
            this.niMain.Icon = this.Icon;
           
#if DEBUG
            traceform = new TraceForm();
            traceform.Show();
#endif

            protocol = new PrinterProtocol();
            protocol.SetTraceCallback(traceLog);

            sysConfiged = SysConfig.HasConfigured();
        }

        #region Ballon Tip
        private delegate void SetBallonTipInfoDelegate(string tipText, int tipIcon);

        private void SetBallonTipInfoSynchronized(string tipText, int tipIcon)
        {
            this.niMain.BalloonTipText = tipText;
            this.niMain.BalloonTipIcon = ToolTipIcon.Info;
            this.niMain.ShowBalloonTip(1000);
        }

        private void SetBallonTipInfo(string tipText, int tipIcon)
        {
            this.Invoke(new SetBallonTipInfoDelegate(SetBallonTipInfoSynchronized), new object[] { tipText, tipIcon });
        }
        #endregion

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetting setting = new frmSetting();
            setting.ShowDialog();
            sysConfiged = SysConfig.HasConfigured();
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FormClosing -= frmMain_FormClosing;
            protocol.Exit();
            this.Close();
            //Application.Exit();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (inProcess)
            {
                btnMannal.Enabled = false;
                manualSendToolStripMenuItem.Enabled = false;
                inProcess = false;
                protocol.CloseLink();
            }
            else
            {
                if (!sysConfiged)
                {
                    MessageBox.Show(Properties.Resources.STR_DO_SETTING);
                    return;
                }

                btnMannal.Enabled = true;
                manualSendToolStripMenuItem.Enabled = true;
                inProcess = true;
                protocol.Initialize();
                protocol.OpenLink();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.showToolStripMenuItem.Enabled = true;
#if DEBUG
            traceform.Hide();
#endif
            e.Cancel = true;
            SetBallonTipInfo(Properties.Resources.STR_FORM_HIDE, 0);
        }

        // Trace and Debug
        public void traceLog(string s)
        {
            System.Diagnostics.Trace.Write(s);
        }

        private void btnMannal_Click(object sender, EventArgs e)
        {
            protocol.SendTask();
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
#if DEBUG
            traceform.Show();
#endif
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.showToolStripMenuItem.Enabled = false;


        }




    }
}
